<template>
  <van-goods-action>
    <van-goods-action-icon icon="chat-o" text="客服" dot />
    <van-goods-action-icon icon="cart-o" text="购物车" :badge="badge" @click="gobasket"/>
    <van-goods-action-icon icon="shop-o" text="店铺" badge="12" />
    <van-goods-action-button type="warning" text="加入购物车" @click="addBasket"/>
    <van-goods-action-button type="danger" text="立即购买" @click="addBaCheckOrd"/>
  </van-goods-action>
</template>

<script>

import Ax from "../../views/ProductDetail/axios";
import baAx from "../../views/basket/axios"
import { mapState } from "vuex";


const baax = new baAx()
const ax = new Ax()

export default {
  data(){
    return {
      badge:0,
      basket_id:"",

    }
  },
  name: "bottumbar",
  props:["basketdata"],
  methods:{
    gobasket(){
     this.$router.push({name:"basket"})
    },
    addBasket(){
      //判断用户是否有购物车
      ax.makeDetail(this.token).then(response=>{
        if (response.data.id){
          ax.addProductDetail(response.data.id, this.basketdata, this.token).then(response=>{
            if (response.status !== 202){
                  alert("添加失败")

                  return

            }else {
              this.$router.go(0)
            }

          })
        }

      })


    },
    addBaCheckOrd(){
      //先加入购物车在修改购物车
      console.log("直接下单")
    }
  },
  computed:{
   ...mapState("login",["token"]),
    ...mapState("basket", ["basketlength"])
  },
  watch:{
    basketlength(){
      this.$set("badge", this.basketlines)
    }
  },
  created() {
    //获取所有的购物车
    baax.getBasketLines({username:sessionStorage.getItem("userinfo")}).then(response=>{
      this.$set(this, "badge", response.data?response.data.data[0].basket_lines.length:0)
    })
  }

}
</script>

<style scoped>

</style>